<template>
    <div>
        <mu-button @click="clickEvent">上传</mu-button>
    </div>
</template>
<script>
    import http from "@/util/api"
    export default{
        data(){
            return {
            };
        },
        mounted(){
            let $this = this;
            this.sign((data)=>{
                let appId = data["appId"],
                    timestamp= data["timestamp"],
                    nonceStr= data["nonceStr"],
                    signature= data["signature"];
                $this.init(appId,timestamp,nonceStr,signature);
            });
        },
        methods:{
            upload(id){
                let $this = this;
                http.post("v1.tool.upload.file",{id:id},(resp)=>{
                    if(resp.error == 0 ){
                        let url = resp.data.url;
                        $this.$emit("callback",url);
                    }
                });
            },
            sign(id,cb){
                http.get("v1.tool.upload.sign",(resp)=>{
                    if(resp.error == 0 ){
                        let sign = resp.data.sign;
                        cb(sign);
                    }
                });
            },
            clickEvent(){
                let $this = this;
                if( !(window.wx) ){
                    let url = ("http://ovu4ce44g.bkt.clouddn.com/uploads/1810/061001368551.png");
                    $this.$emit("callback",url);
                }else{
                    wx.chooseImage({
                        count: 1,
                        sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
                        sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
                        success: function (res) {
                            let localIds = res.localIds; // 返回选定照片的本地ID列表，localId可以作为img标签的src属性显示图片
                            $this.upload(localIds[0]);
                        }
                    });
                }
            },
            init(appId,timestamp,nonceStr,signature) {
                let $this = this;
                wx.config({
                        debug: false,
                        appId: appId,
                        timestamp: timestamp,
                        nonceStr: nonceStr,
                        signature: signature,
                        jsApiList: [
                        // 所有要调用的 API 都要加到这个列表中
                        "chooseImage",
                        "previewImage",
                        "uploadImage",
                        "downloadImage"
                    ]
                });
                wx.ready(function () {
                    $('#uploadBtn').click(function () {
                        wx.chooseImage({
                            count: 1,
                            sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
                            sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
                            success: function (res) {
                                let localIds = res.localIds; // 返回选定照片的本地ID列表，localId可以作为img标签的src属性显示图片
                                $this.upload(localIds[0]);
                            }
                        });
                    });
                });
            }
        }
    }
</script>